﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebEntities;
using WebEntities.Models.WebInfo;

namespace com.subb.WebInformation
{
    public class WI_JobsDAL
    {
        public List<WI_Jobs> GetAllJobs(int start, int limit, out int ttlCount)
        {
            ttlCount = 0;
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.WI_Jobs.OrderByDescending(c => c.Sort ?? 0).ThenByDescending(c => c.CreateTime);
                ttlCount = query.Count();
                List<WI_Jobs> joblist = query.Skip(start).Take(limit).ToList();
                return joblist;
            }
        }

        public List<WI_Jobs> GetAllJobs(JobShowStatus isShow, int start, int limit, out int ttlCount)
        {
            ttlCount = 0;
            byte b = (byte)isShow;
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.WI_Jobs.Where(c => c.IsShow == (byte)isShow).OrderByDescending(c => c.Sort ?? 0).ThenByDescending(c => c.CreateTime);
                ttlCount = query.Count();
                List<WI_Jobs> joblist = query.Skip(start).Take(limit).ToList();
                return joblist;
            }
        }

        public WI_Jobs GetJob(int jobId)
        {
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                WI_Jobs job = ctx.WI_Jobs.FirstOrDefault(c => c.JobID == jobId);
                return job;
            }
        }

        public void CreateJob(Jobs vjob)
        {
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                WI_Jobs newjob = new WI_Jobs
                {
                    JobName = vjob.JobName,
                    JobAddress = vjob.JobAddress,
                    JobPepleNum = vjob.JobPepleNum,
                    JobSex = vjob.JobSex,
                    WorkExperience = vjob.WorkExperience,
                    EduBackGround = vjob.EduBackGround,
                    CreateTime = DateTime.Now,
                    PubTime = vjob.PubTime,
                    LimitTime = vjob.LimitTime,
                    IsShow = vjob.IsShow,
                    Sort = vjob.Sort,
                    JobDes = vjob.JobDes,
                    CompanyName = vjob.CompanyName,
                    CompanyIntro = vjob.CompanyIntro
                };
                ctx.WI_Jobs.AddObject(newjob);
                ctx.SaveChanges();
            }
        }

        public bool UpdateJob(int jobId, Jobs vjob)
        {
            bool result = false;
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                WI_Jobs wijob = ctx.WI_Jobs.FirstOrDefault(c => c.JobID == jobId);
                if (wijob != null)
                {
                    wijob.JobName = vjob.JobName;
                    wijob.JobAddress = vjob.JobAddress;
                    wijob.JobPepleNum = vjob.JobPepleNum;
                    wijob.JobSex = vjob.JobSex;
                    wijob.WorkExperience = vjob.WorkExperience;
                    wijob.EduBackGround = vjob.EduBackGround;
                    wijob.PubTime = vjob.PubTime;
                    wijob.LimitTime = vjob.LimitTime;
                    wijob.IsShow = vjob.IsShow;
                    wijob.Sort = vjob.Sort;
                    wijob.JobDes = vjob.JobDes;
                    wijob.CompanyName = vjob.CompanyName;
                    wijob.CompanyIntro = vjob.CompanyIntro;
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }

        public bool RemoveJob(int jobId)
        {
            bool result = false;
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                WI_Jobs wijob = ctx.WI_Jobs.FirstOrDefault(c => c.JobID == jobId);
                if (wijob != null)
                {
                    ctx.WI_Jobs.DeleteObject(wijob);
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }

        public List<WI_Jobs> SearchByPosition(string key, int start, int limit, out int ttlCount)
        {
            WebsiteDbEntities ctx = new WebsiteDbEntities();
            var query = ctx.WI_Jobs.Where(c => c.JobName.Contains(key) && c.IsShow == (byte)JobShowStatus.Show).OrderByDescending(c => c.Sort ?? 0).ThenByDescending(c => c.JobID);
            ttlCount = query.Count();
            List<WI_Jobs> jobList = query.Skip(start).Take(limit).ToList();
            return jobList;
        }

        public List<WI_Jobs> SearchByCompany(string key, int start, int limit, out int ttlCount)
        {
            WebsiteDbEntities ctx = new WebsiteDbEntities();
            var query = ctx.WI_Jobs.Where(c => c.CompanyName.Contains(key) && c.IsShow == (byte)JobShowStatus.Show).OrderByDescending(c => c.Sort ?? 0).ThenByDescending(c => c.JobID);
            ttlCount = query.Count();
            List<WI_Jobs> jobList = query.Skip(start).Take(limit).ToList();
            return jobList;
        }
    }
}
